﻿using System;
using System.Collections.Generic;
using System.Data;
using WorkFlow.DBProxy;

namespace WorkFlow.Engine.NSPermission
{
    /// <summary>
    ///     组织
    /// </summary>
    public class WfOrganization
    {
        #region 变量

        public readonly List<WfOrgStruct> _specificOrgsCollection = null;

        public int ID { get; private set; }

        public string Name { get; private set; }

        public int Superior { get; private set; } //上级组织id

        public bool IsCreated { get; private set; }

        public int IsTopLevel { get; private set; } //is top org

        public int Type { get; private set; }

        public DateTime LastModifiedTime { get; set; }

        public int OrgLevel { get; private set; }

        public int IsAbstractOrg { get; private set; }

        public int AbstractOrg { get; set; } //当IsAbstractOrg为 false时才设置该字段

        #endregion 变量

        #region constructor

        /// <summary>
        ///     organization constructor
        /// </summary>
        /// <param name="orgID"></param>
        public WfOrganization(int orgID)
        {
            //-------------先设置组织信息
            try
            {
                DataTable dt =
                    DB.MapToDataTable(
                        string.Format(@"select F_ID,F_NAME,F_SUPERIOR_ID AS SUP,F_IS_TOP,F_LEVEL,F_TYPE,F_ABSTRACT_ORG
from t_organizations WHERE f_id={0}", orgID));
                if (dt == null || dt.Rows.Count < 1)
                {
                    IsCreated = false;
                    return;
                }
                ID = Convert.ToInt32(dt.Rows[0]["F_ID"]);

                //如果是抽象单位
                if (int.Parse(dt.Rows[0]["F_TYPE"].ToString()) == WfFieldCode.DbTypeAbstractOrg)
                {
                    IsAbstractOrg = WfFieldCode.DbTrue;
                    Name = dt.Rows[0]["F_NAME"].ToString();
                    Type = Convert.ToInt32(dt.Rows[0]["F_TYPE"]);

                    //----获取属于该抽象单位的具体单位
                    DataTable dts = DB.MapToDataTable(string.Format(@"select F_ID,F_NAME from t_organizations
where F_ABSTRACT_ORG={0}", ID));
                    if (dts == null || dts.Rows.Count < 1)
                    {
                        IsCreated = false;
                        return;
                    }

                    _specificOrgsCollection = new List<WfOrgStruct>();
                    int rows = dts.Rows.Count;
                    for (int i = 0; i < rows; i++)
                    {
                        _specificOrgsCollection.Add(new WfOrgStruct
                        {
                            _id = Convert.ToInt32(dts.Rows[i]["F_ID"]),
                            _name = dts.Rows[i]["F_NAME"].ToString(),
                            _abstractID = ID,
                            _runActID = -1
                        });
                    }
                } //end if it is abstract orgs
                else
                {
                    //if it is specific org
                    Name = dt.Rows[0]["F_NAME"].ToString();
                    Superior = Convert.ToInt32(dt.Rows[0]["SUP"]);
                    IsTopLevel = Convert.ToInt32(dt.Rows[0]["F_IS_TOP"]);
                    OrgLevel = Convert.ToInt32(dt.Rows[0]["F_LEVEL"]);
                    Type = Convert.ToInt32(dt.Rows[0]["F_TYPE"]);
                    AbstractOrg = Convert.ToInt32(dt.Rows[0]["F_ABSTRACT_ORG"]);
                    IsAbstractOrg = WfFieldCode.DbFalse;
                }

                LastModifiedTime = DateTime.Now;
            }
            catch
            {
                IsCreated = false;
                return;
            }
            IsCreated = true;
        }

        #endregion constructor

        #region 自定义方法

        public bool IsParent(int childOrgID)
        {
            int count = _specificOrgsCollection == null ? 0 : _specificOrgsCollection.Count;
            if (count == 0) return false;
            for (int i = 0; i < count; i++)
            {
                // ReSharper disable once PossibleNullReferenceException
                if (_specificOrgsCollection[i]._id == childOrgID) return true;
            }
            return false;
        }

        #endregion 自定义方法
    }
}